package com.pzh.guli.service.sms.service.imp;

import com.pzh.guli.service.sms.pojo.SmsProperties;
import com.pzh.guli.service.sms.service.SmsService;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class SmsServiceImpI implements SmsService {
    @Autowired
    SmsProperties smsProperties;
    //发送验证码
    @Override
    public void send(String tel, String code) throws ClientException {
        DefaultProfile profile=DefaultProfile.getProfile(smsProperties.getEndpoint(),smsProperties.getKeyid(),smsProperties.getKeysercret());
        IAcsClient client=new DefaultAcsClient(profile);
        SendSmsRequest request=new SendSmsRequest();
        request.setSignName(smsProperties.getSignName());
        request.setTemplateCode(smsProperties.getTemplateCode());
        request.setPhoneNumbers(tel);//设置手机号
        Map<String,String> param=new HashMap<>();
        param.put("code",code);
        Gson gson=new Gson();
        String json = gson.toJson(param);
        request.setTemplateParam(json); //设置随机验证码
        //发送过程
        SendSmsResponse response=client.getAcsResponse(request);
        log.info(response.getMessage());
    }
}
